<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>控制器</title>
    <style>
      #controller-area {
        position: relative;
        width: 500px;
        height: 360px;
        background-color: antiquewhite;
        margin: 20px auto;
      }
      #controller {
        position: absolute;
        top: calc(50% - 25px);
        left: calc(50% - 25px);
        width: 50px;
        height: 50px;
        background-color: blueviolet;
        transform: rotate(30deg);
      }
    </style>
  </head>
  <body>
    <div id="controller-area">
      <div id="controller"></div>
    </div>
    <script>
      var controllerDom = document.getElementById("controller");
      var cleft = parseInt(getComputedStyle(controllerDom).left);
      var ctop = parseInt(getComputedStyle(controllerDom).top);

      document.addEventListener("keydown", (e) => {
        if (e.key === "ArrowLeft") {
          cleft = cleft - 10;
        } else if (e.key === "ArrowRight") {
          cleft = cleft + 10;
        } else if (e.key === "ArrowUp") {
          ctop = ctop - 10;
        } else if (e.key === "ArrowDown") {
          ctop = ctop + 10;
        }
        if (cleft < 0) {
          cleft = 0;
        } else if (cleft > 450) {
          cleft = 450;
        }
        if (ctop < 0) {
          ctop = 0;
        } else if (ctop > 310) {
          ctop = 310;
        }
        controllerDom.style.left = cleft + "px";
        controllerDom.style.top = ctop + "px";
      });

      //   document.addEventListener("keyup", (e) => {
      //     console.log(666.002, e);
      //   });
    </script>
  </body>
</html>
